package com.cs.noTender.controller;

import com.cs.noTender.domain.CsQuotation;
import com.cs.noTender.domain.NonTenderProject;
import com.cs.noTender.service.NonTenderProjectService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：zrx
 * @Package：com.cs.service
 * @Project：RuoYi-Cloud
 * @name：NonTenderProjectController
 * @Date：2024/12/9 11:14
 * @Filename：NonTenderProjectController
 */
@RestController
@RequestMapping("/pro")
public class NonTenderProjectController extends BaseController {
    private final NonTenderProjectService nonTenderProjectService;

    public NonTenderProjectController(NonTenderProjectService nonTenderProjectService) {
        this.nonTenderProjectService = nonTenderProjectService;
    }

    /**
     * 查询非招标项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(NonTenderProject nonTenderProject) {
        startPage();
        List<NonTenderProject> list = nonTenderProjectService.litNonTenderProject(nonTenderProject);
        return getDataTable(list);
    }

    /**
     * 获取附件详细信息
     */
    @GetMapping("/selectCom")
    public AjaxResult selectCom(Integer aid) {
        return success(nonTenderProjectService.getCsPubAttachmentsByAid(aid));
    }

    /**
     * 获取非招标项目详细信息
     */
    @GetMapping(value = "/{gid}")
    public AjaxResult getInfo(@PathVariable("gid") Integer gid) {
        return success(nonTenderProjectService.getNonTenderProjectByGid(gid));
    }

    /**
     * 查询项目报价
     */
    @GetMapping("/selectQuotation")
    public TableDataInfo selectQuotation(String gfId) {
        List<CsQuotation> list = nonTenderProjectService.listQuotation(gfId);
        System.out.println("报价：" + list);
        return getDataTable(list);
    }

    /**
     * 修改非招标项目
     */
    @PutMapping
    public AjaxResult edit(@RequestBody NonTenderProject nonTenderProject) {
        int i = nonTenderProjectService.updateNonTenderProject(nonTenderProject);
        if (i > 0) {
            int i1;
            if (nonTenderProject.getgRelease() == 2) {
                int i2 = nonTenderProjectService.updateNonTenderProject(nonTenderProject);
                if (i2 == 0) {
                    return AjaxResult.error("发布失败");
                } else {
                    return AjaxResult.success("发布成功");
                }
            } else {
                if (nonTenderProject.getCsPubAttachments().getAnId() == null) {
                    i1 = nonTenderProjectService.insertCsPubAttachments(nonTenderProject.getCsPubAttachments());
                } else {
                    i1 = nonTenderProjectService.updateCsPubAttachments(nonTenderProject.getCsPubAttachments());
                }
                if (i1 == 0) {
                    return AjaxResult.error("发布失败");
                }
                return AjaxResult.success("发布成功");
            }
        }
        return AjaxResult.error("发布失败");
    }

    /**
     * 新增报价单
     */
    @PostMapping("/addCom")
    public AjaxResult addCom(@RequestBody CsQuotation csQuotations) {
        return success(nonTenderProjectService.insertCsQuotation(csQuotations));
    }

    /**
     * 删除报价单
     */
    @DeleteMapping("/delByGfId")
    public AjaxResult remove(String gfId) {
        return success(nonTenderProjectService.deleteCsQuotationByGfIds(gfId));
    }
}
